package Model.Universe;

import java.awt.Point;
import java.util.ArrayList;

import Controller.Game.GamePanelController;
import Model.Visible.Fleet;
import Model.Visible.Planet;
import Utils.ConstantsImplement;

	/**
	 * 
	 * This class represents a Universe, collection of Planets:<dd>
	 * 	- Generates and Distributes the Planets
	 *</dd>
	 *  @autor Luiz
	 *  
	 */

public class Universe implements ConstantsImplement {

	private ArrayList<Planet> planets_ = new ArrayList<Planet>();
	private Point[] positions = new Point[NUM_PLANETS];
	private int nextposition = 0;
	private ArrayList<Fleet> fleets;
	

	/**
	 * Universe Constructor 
	 */
	public Universe(GamePanelController gamePanelController) {
		fleets = new ArrayList<Fleet>();
		for (int i = 0; i < NUM_PLANETS; i++) {
//			System.out.println(i);
			planets_.add(new Planet(i,
					generateRandPlanetPosition(), gamePanelController));
			nextposition++;
//			System.out.println("out-function");
//			System.out.println(i);

		}
	}
	
	public Universe(ArrayList<Planet> planets, GamePanelController gamePanelController) {
		fleets = new ArrayList<Fleet>();
		planets_ = planets;
	}
	
	public Planet getPlanets(int i) {
		return planets_.get(i);
	}
	
	public Planet getPlanets(String hash){
		for(int i=0; i<NUM_PLANETS;i++){
			if(planets_.get(i).hashCode() == Integer.parseInt(hash)){
				return planets_.get(i);
				}
		}
		return null;
	}

	
	public ArrayList<Planet> getPlanets() {
		return planets_;
	}

	public void setPlanets(ArrayList<Planet> planets) {
		this.planets_ = planets;
	}

	/**
	 * Generates a random Planet position in the screen, avoiding the Panel borders
	 * and a radius of others Planets.
	 * 
	 * @return A Point to be assigned to the Planet
	 */
	private Point generateRandPlanetPosition() {

		boolean colision = true;

		positions[this.nextposition] = new Point(0, 0);

		while (colision) {
			//generates random points
			positions[this.nextposition].x = LESSER_UNIVERSE_BORDER_X
					+ (int) (Math.random() * GREATER_UNIVERSE_BORDER_X);
			positions[this.nextposition].y = LESSER_UNIVERSE_BORDER_Y
					+ (int) (Math.random() * GREATER_UNIVERSE_BORDER_Y);
			colision = false;

			for (int i = 0; i < this.nextposition ; i++) { 
				if (Point.distance(positions[this.nextposition].x, 
						positions[this.nextposition].y, 
						positions[i].x, 
						positions[i].y) < MIN_PLANETS_DISTANCE) {
					colision = true;
				}
			}
		}

		//System.out.println(positions[nextposition]);
		return positions[this.nextposition ];
	}
	
	public void setFleets(ArrayList<Fleet> fleets) {
		this.fleets = fleets;
	}
	
	public ArrayList<Fleet> getFleets() {
		return fleets;
	}
	public void addFleet(Fleet fleet) {
		fleet.setMoving_(true);
		fleets.add(fleet);
	}
	
	public void startPlanetTimer(){
		for(int i=0; i<NUM_PLANETS;i++){
			planets_.get(i).startTimer();
		}
	}
	
	public void stopPlanetTimer(){
		for(int i=0; i<NUM_PLANETS;i++){
			planets_.get(i).stopTimer();
		}
	}
	
	public Point[] getPositions() {
		return positions;
	}
	
}